package com.bookstore.pojo;

import java.util.List;

public class Page<T> {
    //当前页，用户的请求参数
    private int  pageNo;
    //总页码：根据数据库表的总记录数计算的
    private int totalPage;
//    总记录数,
    private int totalCount;
    //每页显示的记录数
    private int pageSize=4;
//    数据库查询的索引,根据用户的请求
    private int index;
    //判断是否有上一页
    private boolean hashNext;
    //判断是否有下一页
    private boolean hashPrev;
//    分页的实际数据，根据用户请求查询而得
    private List<T> pageData;

    private String url;

    public Page() {
    }

    public Page(int pageNo, int totalPage, int totalCount, int pageSize, int index, boolean hashNext, boolean hashPrev, List<T> pageData, String url) {
        this.pageNo = pageNo;
        this.totalPage = totalPage;
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.index = index;
        this.hashNext = hashNext;
        this.hashPrev = hashPrev;
        this.pageData = pageData;
        this.url=url;
    }

    public int getPageNo() {
        pageNo=pageNo>0?pageNo:1;
        pageNo=pageNo>this.getTotalPage()?this.getTotalPage():pageNo;
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public int getTotalPage() {
        //计算实际的总页数
        int t=this.getTotalCount()/this.getPageSize();
        if ((this.getTotalCount()%this.getPageSize()!=0)) {
            t++;
        }
        return t;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    //计算得出index
    public int getIndex() {
        //每页显示4条
        //0-3为一页 4-7为一页
        int i=(getPageNo()-1)*(getPageSize());
        if (i<0) i=0;//可以将sqlException的异常
        return i;
    }
    //判断是否有下一页
    public boolean isHashNext() {
        return this.getPageNo()<this.getTotalPage();
    }
    //判断是否有上一页
    public boolean isHashPrev() {
        return this.getPageNo()>1;
    }

    public List<T> getPageData() {
        return pageData;
    }

    public void setPageData(List<T> pageData) {
        this.pageData = pageData;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return "Page{" +
                "pageNo=" + pageNo +
                ", totalPage=" + totalPage +
                ", totalCount=" + totalCount +
                ", pageSize=" + pageSize +
                ", index=" + index +
                ", hashNext=" + hashNext +
                ", hashPrev=" + hashPrev +
                ", pageData=" + pageData +
                ","+url+
                '}';
    }
}
